package com.banzhi.shop.utils;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.Log;
import android.view.Display;
import android.view.View;

/**
 * Created by HT on 2017/5/20.
 */

public class FuzzyUtils {
    private Activity activity;

    public FuzzyUtils(Activity activity) {
        this.activity = activity;
    }

    public Bitmap myShot() {
        // 获取windows中最顶层的view
        View view = activity.getWindow().getDecorView();
        view.buildDrawingCache();

        // 获取状态栏高度
        Rect rect = new Rect();
        view.getWindowVisibleDisplayFrame(rect);
        int statusBarHeights = rect.top;
        Display display = activity.getWindowManager().getDefaultDisplay();

        // 获取屏幕宽和高
        int widths = display.getWidth();
        int heights = display.getHeight();

        // 允许当前窗口保存缓存信息
        view.setDrawingCacheEnabled(true);

        // 去掉状态栏
        Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,
                statusBarHeights, widths, heights - statusBarHeights);

        // 销毁缓存信息
        view.destroyDrawingCache();

        return bmp;
    }

    public Bitmap blur() {
        long startMs = System.currentTimeMillis();
        float radius = 122;
        float scaleFactor = 8;
        View view = activity.getWindow().getDecorView();
        Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / scaleFactor), (int) (view.getMeasuredHeight() / scaleFactor), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(overlay);
        canvas.translate(-view.getLeft() / scaleFactor, -view.getTop() / scaleFactor);
        canvas.scale(1 / scaleFactor, 1 / scaleFactor);
        Paint paint = new Paint();
        paint.setFlags(Paint.FILTER_BITMAP_FLAG);
        canvas.drawBitmap(myShot(), 0, 0, paint);
        overlay = FastBlur.doBlur(overlay, (int) radius, true);
        Log.e("info", System.currentTimeMillis() - startMs + "");
        return overlay;
//        view.setBackground(new BitmapDrawable(activity.getResources(), overlay));
    }
}
